package com.example.cxx.zhuce;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

public class ListViewDemo1 extends Activity {
    //声明新闻列表集合为全局变量
    private ArrayList<NewsBean> newsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_demo1);
        newsList=new ArrayList<NewsBean>();
        //为集合赋值
        newsList=init();

        //第一步：找到ListView的控件
        ListView listView=(ListView) findViewById(R.id.lv_main);
        //第二步：创建适配器Adapter对象
        MyAdapter adapter=new MyAdapter();
        //第三步：为ListView添加适配器
        listView.setAdapter(adapter);
        //将top.xml文件转换成View
        View top=View.inflate(getApplicationContext(),R.layout.top,null);
        listView.addHeaderView(top);//参数为一个视图
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击条目后，显示详细内容
                Intent intent=new Intent(ListViewDemo1.this,NewsDetail.class);
                startActivity(intent);
            }
        });
    }

    //Adapter适配器
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {//显示条目

            return newsList.size();
        }

        @Override
        public Object getItem(int position) {

            return null;
        }

        @Override
        public long getItemId(int position) {

            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //将item1转换
            NewsBean news=newsList.get(position);
            ViewHolder holder=null;
            if (convertView==null){
                convertView=View.inflate(getApplicationContext(),R.layout.item1,null);//inflate：打气筒,将布局文件转换为一个View对象
               /* Log.i("main","创建"+position);*/
                holder=new ViewHolder();
                holder.tvTitle=convertView.findViewById(R.id.tv_title);
                holder.pic=convertView.findViewById(R.id.iv_icon);
                convertView.setTag(holder);
        }else{
               /* Log.i("main","复用"+position);*/
                holder= (ViewHolder) convertView.getTag();
            }
            holder.tvTitle.setText(news.getTitle());
            holder.pic.setImageResource(news.getPic());
           /* TextView tvTitle=convertView.findViewById(R.id.tv_title);
            ImageView ivIcon=convertView.findViewById(R.id.iv_icon);*/
            TextView tvBottom=convertView.findViewById(R.id.tv_bottom);
            //为item中的控件赋值
            /*tvTitle.setText((position+1)+"第"+(position+1)+"行");
            ivIcon.setImageResource(R.drawable.action);*/



            tvBottom.setText(news.getTishi());

            return convertView;
        }
    }

       public ArrayList<NewsBean> init(){
        ArrayList<NewsBean> list=new ArrayList<NewsBean>();
        NewsBean news1=new NewsBean("今天星期一",R.drawable.action,"人民日报");
        NewsBean news2=new NewsBean("上海新增境外输入52例其中51例从俄乘同一航班",R.drawable.action,"健康上海");
        NewsBean news3=new NewsBean("发动机是部分地方就看电视看见看见风科技将尽快的刷卡缴费电视剧",R.drawable.action,"央视新闻");
        NewsBean news4=new NewsBean("风刀霜剑孵化基地尸鬼封尽电话客服河口DNF",R.drawable.action,"健康中国");
        NewsBean news5=new NewsBean("粉碎机倒海翻江都是废话第四节覅丰富金额我合计二维火",R.drawable.action,"人民日报");
        NewsBean news6=new NewsBean("净收入户如何香孵化基地是否可借独守空房",R.drawable.action,"健康上海");
        NewsBean news7=new NewsBean("顾业务股份热乎饭很多时间好看就范德萨范德萨范德萨",R.drawable.action,"央视新闻");
        NewsBean news8=new NewsBean("UI如复活甲sad吉安市第四大街时尚大师",R.drawable.action,"健康中国");
        NewsBean news9=new NewsBean("UI如复活甲sad吉安市第四大街时尚大师",R.drawable.action,"健康中国");
        NewsBean news10=new NewsBean("UI如复活甲sad吉安市第四大街时尚大师",R.drawable.action,"健康中国");
        NewsBean news11=new NewsBean("UI如复活甲sad吉安市第四大街时尚大师",R.drawable.action,"健康中国");

        Collections.addAll(list,news1,news2,news3,news4,news5,news6,news7,news8,news9,news10,news11,news2,news3,news4);
        return list;
    }
}
